package com.pilates

import java.text.SimpleDateFormat;

class TurmaController {
	
	def scaffold = Turma
	
	def list = {
		//params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[turmaInstanceList: Turma.list(params), turmaInstanceTotal: Turma.count()]
	}
	
	def create = {
		def turmaInstance = new Turma()
		turmaInstance.properties = params
		return [turmaInstance: turmaInstance]
	}
	
	def save = {
		def turmaInstance = new Turma(params)
		turmaInstance = setMaskName( turmaInstance );
		if (turmaInstance.save(flush: false)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'turma.label', default: 'Turma'), turmaInstance.id])}"
			redirect(action: "edit", id: turmaInstance.id)
		}
		else {
			render(view: "create", model: [turmaInstance: turmaInstance])
		}
	}
	
	
	public Turma setMaskName(Turma turma){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		//if(turma.getNome() != null && "" != turma.getNome()){
			turma.nome  = turma.getDiaSemana().toString()+" - "+sdf.format(turma.getHorarioInicio())+" > "+sdf.format(turma.getHorarioFinal())
		//}
		
		return turma
	}
}
